package com.example.androidcompletesec;

import android.app.ExpandableListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

public class MainActivity extends ExpandableListActivity{
	private String[] mainTitles;
	private String[][] subTitles;
	private String[] pkgLists;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mainTitles = getResources().getStringArray(R.array.titles);
		pkgLists = getResources().getStringArray(R.array.pkglist);
		
		subTitles = new String[mainTitles.length][];
		for(int i=0;i<mainTitles.length;i++){
			int resId = getResources().getIdentifier("subtitles"+i, "array", getPackageName()); //리소스 아이디를 넘겨준다.(R.array..subtitles.. )
																	//데이터 타입 // 패키지명
			subTitles[i] = getResources().getStringArray(resId);
		}
		for(int i=0;i<mainTitles.length;i++){
			Log.d("Test", mainTitles[i]);
			for(int j=0;j<subTitles[i].length;j++){
				Log.d("Test", subTitles[i][j]);	
			}
		}
		
		MyExpandableListAdapter adapter = new MyExpandableListAdapter();
		setListAdapter(adapter);
		
		
	}
	
	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		super.onChildClick(parent, v, groupPosition, childPosition, id);
		
		Intent intent = new Intent();
		//com.example.androidcompletesec.fragment.FragmentEx;
		intent.setClassName(getPackageName(), getPackageName()+"."+pkgLists[groupPosition]+"."+subTitles[groupPosition][childPosition]);
		startActivity(intent);
		return true; 
	}

	class MyExpandableListAdapter extends BaseExpandableListAdapter{

		@Override
		public int getGroupCount() { //메인 타이틀 갯수..
			return mainTitles.length;
		}

		@Override
		public int getChildrenCount(int groupPosition) { //메인 하위..
			return subTitles[groupPosition].length;
		}

		@Override
		public Object getGroup(int groupPosition) { //해당하는 그룹 가져오기..
			return mainTitles[groupPosition];
		}

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return subTitles[groupPosition][childPosition];
		}

		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		@Override
		public boolean hasStableIds() { 
			return true;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			TextView tv = new TextView(MainActivity.this);
			tv.setTextSize(30);
			tv.setGravity(Gravity.CENTER_VERTICAL|Gravity.START);
			tv.setPadding(36, 0, 0, 0);
			tv.setText(getGroup(groupPosition).toString());
			return tv;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); //
			TextView tv = (TextView) inflater.inflate(android.R.layout.simple_list_item_1, null);
			tv.setPadding(36, 0, 0, 0);
			tv.setText(getChild(groupPosition, childPosition).toString());
			return tv;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return true;
		}
	}
}
